package com.cool.shark.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cool.shark.service.BannerService;
import com.cool.shark.service.GoodsService;
import com.cool.shark.utils.UploadGiteeImgBedUtils;
import com.cool.shark.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@Slf4j
public class UploadController {
    @Autowired
    private BannerService bservice;
    @Autowired
    private GoodsService gservice;

    /**
     * 上传轮播图图片
     *
     * @param multipartFile 文件对象
     * @throws IOException
     */
    @PostMapping("/banner")
    public String uploadBannerImg(@RequestParam("picFile") MultipartFile multipartFile) throws IOException {
        log.info("开始处理上传图片,参数:{}", multipartFile);
        String url = bservice.uploadImg(multipartFile);
        return url;
    }

    /**
     * 上传商品图片
     *
     * @param multipartFile 上传的图片文件，使用MultipartFile类型接收
     * @return 返回图片的下载地址字符串。如果上传过程中出现问题，返回相应的错误信息。
     * @throws IOException 如果处理文件过程中发生IO错误
     */
    @PostMapping("/goods")
    public String uploadGoodsImg(@RequestParam("picFile") MultipartFile multipartFile) throws IOException {
        log.info("开始处理上传图片,参数:{}", multipartFile);
        String url = gservice.uploadGoodsImg(multipartFile);
        return url;
    }
}
